<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">
        {{spu.spuName}}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>

      <el-form-item label="平台属性">
        <el-form label-width="80px" :inline=true>
          <el-form-item :label="attr.attrName" v-for="attr in attrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValue">
              <el-option :label="value.valueName" :value="`${attr.id}:${value.id}`" v-for="value in attr.attrValueList" :key="value.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form label-width="80px" :inline=true>
          <el-form-item :label="sale.saleAttrName" v-for="sale in spuSaleAttrList" :key="sale.id">
            <el-select placeholder="请选择" v-model="sale.saleIdAndValue">
              <el-option :label="value.saleAttrValueName" :value="`${sale.id}:${value.id}`" v-for="value in sale.spuSaleAttrValueList" :key="value.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片列表">
        <el-table style="width: 100%" :data="imageList" @selection-change="handleSelectionChange">

          <el-table-column type="selection" width="80">
          </el-table-column>

          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
            </template>
          </el-table-column>

          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>

          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button type="primary" v-if="row.isDefault==0" @click="changeDefault(row)">设置默认</el-button>
              <el-button type="success" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "SkuForm",
  data() {
    return {
      imageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      skuInfo: {
        category3Id: 0,
        price: 0,
        spuId: 0,
        tmId: 0,
        skuName: "",
        weight: "",
        skuDefaultImg: "",
        skuDesc: "",
        skuAttrValueList: [],
        skuImageList: [],
        skuSaleAttrValueList: [],
      },
      spu: {},
      // 收集图片的数据字段
      imgList: [],
    };
  },
  computed: {
    ...mapGetters(["levelOneId", "levelTwoId", "levelThreeId"]),
  },
  methods: {
    async getData(row) {
      console.log(row);
      this.skuInfo.category3Id = row.category3Id;
      this.skuInfo.tmId = row.tmId;
      this.skuInfo.spuId = row.id;
      this.spu = row;
      // 获取图片
      const imgResult = await this.$API.spu.reqSpuImageList(row.id);
      console.log(imgResult);
      if (imgResult.code == 200) {
        let list = imgResult.data;
        list.forEach((item) => {
          // this.$set("isDafault", 0);
          item.isDefault = 0;
        });
        this.imageList = list;
      }
      // 获取销售属性
      const saleResult = await this.$API.spu.reqSpuSaleAttrList(row.id);
      if (saleResult.code == 200) {
        this.spuSaleAttrList = saleResult.data;
      }
      // 获取平台属性数据
      const attrResult = await this.$API.spu.reqAttrInfoList(
        this.levelOneId,
        this.levelTwoId,
        this.levelThreeId
      );

      if (attrResult.code == 200) {
        this.attrInfoList = attrResult.data;
      }
    },
    // table表格复选框回调方法
    handleSelectionChange(data) {
      this.imgList = data;
    },
    changeDefault(row) {
      this.imageList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    async save() {
      const { attrInfoList, spuSaleAttrList, imgList, skuInfo } = this;
      // 整理平台属性
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValue) {
          const [attrId, valueId] = item.attrIdAndValue.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);
      // 整理销售属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.saleIdAndValue) {
          const [saleAttrId, saleAttrValueId] = item.saleIdAndValue.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      // 整理图片数据
      skuInfo.skuImageList = imgList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
      console.log(skuInfo);
      // 发请求
      const result = await this.$API.spu.reqSaveSku(skuInfo);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "添加成功",
        });
        this.$emit("changeScene", { scene: 0, flag: "" });
      }
    },

    cancel() {
      this.$emit("changeScene", { scene: 0, flag: "" });
      Object.assign(this._data, this.$options.data());
    },
  },
};
</script>